package de.itagile.hypersphere

class SelfReferencingRelationTest extends GroovyTestCase {
	
	def xml = '''
		<ejb-jar id="ejb-jar_ID" version="2.1">
			<enterprise-beans>
				<entity id="Book">
					<ejb-name>Book</ejb-name>
					<local>ejbs.BookLocal</local>
					<prim-key-class>java.lang.Integer</prim-key-class>
					<primkey-field>id</primkey-field>
				</entity>
			</enterprise-beans>
			<relationships>
				<ejb-relation id="0">
					<ejb-relation-name>Book-Book</ejb-relation-name>
					<ejb-relationship-role>
						<ejb-relationship-role-name>
							book_which_cites
						</ejb-relationship-role-name>
						<multiplicity>Many</multiplicity>
						<relationship-role-source>
							<ejb-name>Book</ejb-name>
						</relationship-role-source>
						<cmr-field>
							<cmr-field-name>book_which_cites</cmr-field-name>
							<cmr-field-type>
								java.util.Collection
							</cmr-field-type>
						</cmr-field>
					</ejb-relationship-role>
					<ejb-relationship-role>
						<ejb-relationship-role-name>
							book_which_is_cited
						</ejb-relationship-role-name>
						<multiplicity>Many</multiplicity>
						<relationship-role-source>
							<ejb-name>Book</ejb-name>
						</relationship-role-source>
						<cmr-field>
							<cmr-field-name>book_which_is_cited</cmr-field-name>
							<cmr-field-type>
								java.util.Collection
							</cmr-field-type>
						</cmr-field>
					</ejb-relationship-role>
				</ejb-relation>
			</relationships>
		</ejb-jar>
			'''
	def ejbJar = new EjbJarXmlParser().parseText(xml)
	def ejbTemplate = new EjbTemplate(ejbJar, null, null)
	
	void testGetOppositeEntityOfSelfReference() {
		def relation = ejbTemplate.findRelationsByName("Book")[0]
		assertEquals("Book", relation.getEntityName("Book"));
		assertEquals("Book", relation.getOppositeEntityName("Book"));
	}
	
	void testReturnsOneEntryForOneSelfReferenceRelationship() {
		assertEquals(1, ejbTemplate.findRelationsByName("Book").size())
	}
}